'use strict';
const dayjs = require("dayjs");

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;
  
  const Seats = app.model.define(
    'Seats',
    {
      id: { type: INTEGER, primaryKey: true, autoIncrement: true },
      library_id: { type: INTEGER, allowNull: false },
      floor_id: { type: INTEGER, allowNull: false },
      area_id: { type: INTEGER, allowNull: false },
      name: { type: STRING(50), allowNull: false },
      row_index: { type: INTEGER, defaultValue: 0 },
      column_index: { type: INTEGER, defaultValue: 0 },
      status: { type: STRING(20), defaultValue: 'available' },
      remark: { type: STRING(255) },
      create_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      },
      update_time: {
        type: DATE,
        get() {
          return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
        }
      }
    },
    {
      tableName: 'seats',
      timestamps: true,
      createdAt: 'create_time',
      updatedAt: 'update_time',
      indexes: [
        { fields: ['library_id'] },
        { fields: ['floor_id'] },
        { fields: ['area_id'] }
      ]
    }
  );

  Seats.associate = function() {
    app.model.Seats.belongsTo(app.model.Libraries, { foreignKey: 'library_id', targetKey: 'id', as: 'library' });
    app.model.Seats.belongsTo(app.model.LibraryFloors, { foreignKey: 'floor_id', targetKey: 'id', as: 'floor' });
    app.model.Seats.belongsTo(app.model.LibraryAreas, { foreignKey: 'area_id', targetKey: 'id', as: 'area' });
  };

  return Seats;
};
